<template>
  <div class="aside_nav_menu">
    <el-menu
      :default-active="$route.name"
      class="el-menu-vertical-demo demo"
      @open="handleOpen"
      @close="handleClose"
      :collapse="isCollapse"
      background-color="#fff"
      text-color="#000"
      active-text-color="#ffd04b"
    >
      <h1>{{ isCollapse ? "后台" : "后台管理系统" }}</h1>
      <!-- 没有子菜单 -->
      <el-menu-item
        v-for="item in noChildren"
        :key="item.name"
        :index="item.name"
        @click="changeMenu(item)"
      >
        <i :class="`el-icon-${item.navicon}`"></i>
        <span slot="title">{{ item.label }}</span>
      </el-menu-item>
      <!-- 有子菜单 -->
      <el-submenu
        v-for="item in hasChildren"
        :key="item.label"
        :index="item.label"
      >
        <template slot="title">
          <i :class="`el-icon-${item.navicon}`"></i>
          <span slot="title">{{ item.label }}</span>
        </template>
        <el-menu-item-group v-for="i in item.children" :key="i.name">
          <!-- <span slot="title">分组一</span> -->
          <el-menu-item :index="i.name" @click="changeMenu(i)">
            <i :class="`el-icon-${item.navicon}`"></i>
            <span slot="title">{{ i.label }}</span>
          </el-menu-item>
        </el-menu-item-group>
      </el-submenu>
    </el-menu>
  </div>
</template>

<script>
export default {
  name: "AsideNavMenu",
  data() {
    return {
      is: true,
      NavMenuList: [
        {
          path: "/index",
          name: "index",
          label: "首页",
          id: "1",
          navicon: "house",
        },
        {
          path: "/commodity",
          name: "commodity",
          label: "商品列表",
          id: "2",
          navicon: "refrigerator",
        },
        {
          path: "/management",
          name: "management",
          label: "用户管理",
          id: "3",
          navicon: "edit",
        },
        {
          label: "页面管理",
          navicon: "document",
          children: [
            {
              path: "/documentone",
              name: "documentone",
              label: "页面1",
              id: "4-1",
              navicon: "tickets",
            },
            {
              path: "/documenttwo",
              name: "documenttwo",
              label: "页面2",
              id: "4-2",
              navicon: "document-copy",
            },
          ],
        },
      ],
    };
  },
  computed: {
    // 有子菜单
    hasChildren() {
      return this.NavMenuList.filter((item) => item.children);
    },
    // 没有子菜单
    noChildren() {
      return this.NavMenuList.filter((item) => !item.children);
    },
    // 菜单 展开 关闭
    isCollapse() {
      return this.$store.state.tab.isCollapse;
    },
  },
  mounted() {
  },
  methods: {
    handleOpen(key, keyPath) {
      // console.log(key, keyPath);
    },
    handleClose(key, keyPath) {
      // console.log(key, keyPath);
    },
    // element-ui 跳转方法1
    // @select="changeSidebar"
    // changeSidebar(path) {
    //   this.$router.push(path);
    // },
    // element-ui 跳转方法2
    changeMenu(item) {
      this.$router.push(item.path);
      this.$store.commit("selectMenu", item);
    },
  },

};
</script>

<style lang="less" scoped>
.aside_nav_menu {
  margin-top: 0;
  padding: 10px;
  height: 100vh;
  box-sizing: border-box;

  /deep/.el-menu {
    border-right: 0;
    height: 100%;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12), 0 0 6px rgba(0, 0, 0, 0.04);
    h1 {
      height: 50px;
      line-height: 50px;
      text-align: center;
      font-size: 16px;
      font-weight: 700;
      color: #fff;
      border-radius: 4px;
      background-color: #606060;
      margin: 0;
    }
    // 解决收起没动画问题
    &:not(.el-menu--collapse) {
      width: 240px;
      border-radius: 4px;
    }
  }
}
</style>
